﻿#ifndef __SS_OPENGL_DEVICE_H__
#define __SS_OPENGL_DEVICE_H__
#include <string>

#ifdef _WIN32
#include <Windows.h>
typedef int			HDISPLAY;
#endif
#ifdef SS_USE_X11
#include "GL/glxew.h"
typedef GLXDrawable	HWND;
typedef Display*	HDISPLAY;
#endif
#ifdef SS_USE_GLES2
#include "EGL/egl.h"
#include "GLES2/gl2.h"
typedef EGLNativeWindowType	HWND;
typedef int					HDISPLAY;
#endif

namespace StarSeeker
{
//设备基类
class ssGLDevice
{
public:
	virtual bool InitGLDevice(HDISPLAY hdis, HWND hwnd)=0;
	virtual void SwapBuffers()=0;
	virtual void CloseDevice()=0;
	virtual bool ChooseConfig()=0;
	virtual bool CreateDevice()=0;
	virtual bool SetCurrent()=0;
	std::string		m_error;
};

//win32的opengl
#if defined(_WIN32) && !defined(SS_USE_GLES2)
class ssGLDeviceWin32 : public ssGLDevice
{
public:
	ssGLDeviceWin32();

	bool InitGLDevice(HDISPLAY hdis, HWND hwnd);
	void CloseDevice();
	void SwapBuffers()
	{
		wglSwapLayerBuffers(m_hDC, 1);
	}
	bool ChooseConfig();
	bool CreateDevice();
	bool SetCurrent();

private:
	HWND				m_hWnd;
	HDC					m_hDC;
	HGLRC				m_hGLContext;
};
#endif

//x11的opengl
#ifdef SS_USE_X11
class ssGLDeviceX11 : public ssGLDevice
{
public:
	ssGLDeviceX11();

	bool InitGLDevice(HDISPLAY hdis, HWND hwnd);
	void CloseDevice();
	void SwapBuffers()
	{
		glXSwapBuffers(m_dpy, m_hWnd);
	}
	bool ChooseConfig();
	bool CreateDevice();
	bool SetCurrent();
private:
	GLXDrawable         m_hWnd;
	Display*            m_dpy;
	XVisualInfo*        m_vi;
	GLXContext          m_glContext;
	bool                m_doubleBuffer;
};
#endif

//win32的openglES2.0
#if defined(_WIN32) && defined(SS_USE_GLES2)
#include "EGL/egl.h"
#include "GLES2/gl2.h"
class ssGLES2Device : public ssGLDevice
{
public:
	ssGLES2Device();

	bool InitGLDevice(HDISPLAY hdis, HWND hwnd);
	void CloseDevice();
	void SwapBuffers()
	{
		eglSwapBuffers( m_eglDisplay, m_eglSurface );
	}
	bool ChooseConfig();
	bool CreateDevice();
	bool SetCurrent();
private:
	EGLNativeWindowType	m_hWnd;
	EGLDisplay			m_eglDisplay;
	EGLContext			m_eglContext;
	EGLSurface			m_eglSurface;
	EGLConfig			m_configs[2];
};
#endif

//创建设备
ssGLDevice* CreateGLDevice();

}
#endif
